package com.lyncs.ods.modules.txn.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.lyncs.ods.common.exception.ApiException;
import com.lyncs.ods.common.web.RequestHolder;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.txn.model.OrderInfo;
import com.lyncs.ods.modules.txn.mapper.OrderInfoMapper;
import com.lyncs.ods.modules.txn.model.TxnSkuDetail;
import com.lyncs.ods.modules.txn.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyncs.ods.modules.txn.service.TxnSkuDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 预约单总表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-02-15
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private TxnSkuDetailService txnSkuDetailService;

    @Override
    public OrderInfo getOneById(Long id) {
        OrderInfo orderInfo = this.lambdaQuery().eq(OrderInfo::getId, id).one();
        if (Objects.isNull(orderInfo) || LyncsOdsConstant.OrderStatus.DELETED.getKey().equals(orderInfo.getStatus())) {
            throw new ApiException("订单不存在或已删除，ID：" + id);
        }
        return orderInfo;
    }

    @Override
    public void updateOrderStatus(List<Long> orderIds) {
        if (CollectionUtil.isEmpty(orderIds)) {
            return;
        }
        List<OrderInfo> orderInfoList = this.lambdaQuery().ne(OrderInfo::getStatus, LyncsOdsConstant.OrderStatus.DELETED.getKey()).in(OrderInfo::getId, orderIds).list();
        List<TxnSkuDetail> orderSkuDetails = txnSkuDetailService.getTxnSkuDetails(orderIds, null);

        Map<Long, List<Integer>> orderSkuStatusMap = orderSkuDetails.stream().collect(Collectors.groupingBy(TxnSkuDetail::getOrderId, Collectors.mapping(TxnSkuDetail::getStatus, Collectors.toList())));
        List<OrderInfo> updateList = orderInfoList.stream().map(order -> {
            List<Integer> statusList = orderSkuStatusMap.getOrDefault(order.getId(), null);
            if (CollectionUtil.isEmpty(statusList)) {
                return null;
            }
            statusList = statusList.stream().distinct().collect(Collectors.toList());
            Integer first = statusList.get(0);
            Integer status = statusList.size() == 1 && LyncsOdsConstant.TxnSkuStatus.INIT.getKey().equals(first) ? LyncsOdsConstant.OrderStatus.INIT.getKey() :
                    statusList.size() == 1 && LyncsOdsConstant.TxnSkuStatus.DELIVERED.getKey().equals(first) ? LyncsOdsConstant.OrderStatus.DELIVERED.getKey() : LyncsOdsConstant.OrderStatus.PARTIAL_DELIVERY.getKey();
            return order.setStatus(status)
                    .setLastUpdater(RequestHolder.getUserId())
                    .setUpdateTime(RequestHolder.getRequestDateTime());
        }).collect(Collectors.toList());
        this.updateBatchById(updateList);
    }
}
